<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>校易淘 - 发布商品</title>
  <link rel="stylesheet" href="css/style.css">
  <script src="js/main.js" defer></script>
</head>
<body>

  <!-- 头部 -->
  <header>
    <h1>校易淘二手交易平台</h1>
    <nav>
      <a href="index.html">首页</a>
      <a href="cart.html">购物车</a>
      <a href="publish.html">发布商品</a>
      <a href="myitems.html">我的商品</a>
      <a href="orders.html">我的订单</a>
      <a href="login.html">登录</a>
      <a href="register.html">注册</a>
    </nav>
  </header>

  <div class="container">
    <h2 style="text-align:center; margin-bottom:20px;">发布新商品</h2>

    <form id="publish-form">
      <label>商品名称：</label>
      <input type="text" id="item-title" required>

      <label>价格：</label>
      <input type="number" id="item-price" required>

      <label>图片链接：</label>
      <input type="text" id="item-img" placeholder="如 img/book1.jpg" required>

      <label>描述：</label>
      <textarea id="item-desc" rows="4" required></textarea>

      <label>联系方式：</label>
      <input type="text" id="item-contact" placeholder="手机号或微信" required>

      <button type="submit">发布商品</button>
    </form>
  </div>

  <script>
    const publishForm = document.getElementById("publish-form");

    publishForm.addEventListener("submit", function(e){
      e.preventDefault();

      let newItem = {
        id: Date.now(),
        title: document.getElementById("item-title").value,
        price: parseFloat(document.getElementById("item-price").value),
        img: document.getElementById("item-img").value,
        desc: document.getElementById("item-desc").value,
        contact: document.getElementById("item-contact").value
      };

      let myItems = JSON.parse(localStorage.getItem("myItems")) || [];
      myItems.push(newItem);
      localStorage.setItem("myItems", JSON.stringify(myItems));

      alert("商品发布成功！");
      publishForm.reset();
    });
  </script>

</body>
</html>
